package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.GoodsListVo;
import com.cskaoyan.market.vo.WxGoodsCategoryVo;
import com.cskaoyan.market.vo.WxGoodsListVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther:Han_Xiu_Long
 * @Date: 2024/05/21/19:33
 * @Description:
 */
@Service
public class WxGoodsServiceImpl implements WxGoodsService {
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public Object category(Integer id) {
        //最终返回的类型对象
        WxGoodsCategoryVo wxGoodsCategoryVo = new WxGoodsCategoryVo();
        //currentCategory
        MarketCategoryExample currentCategoryExample = new MarketCategoryExample();
        currentCategoryExample.createCriteria().andIdEqualTo(id);
        MarketCategory currentCategory = marketCategoryMapper.selectOneByExample(currentCategoryExample);
        //pid=0
        Integer currentPid = currentCategory.getPid();
        //currentId=100500
        Integer currentId = currentCategory.getId();
        if (currentPid == 0) {
            MarketCategoryExample categoryExample = new MarketCategoryExample();
            categoryExample.createCriteria().andPidEqualTo(currentId);//currentId=100500
            List<MarketCategory> categoryList = marketCategoryMapper.selectByExample(categoryExample);
            int endIndex = Math.min(categoryList.size(), 1);
            List<MarketCategory> firstCategoryList = categoryList.subList(0, endIndex);
            currentPid = firstCategoryList.get(0).getPid();
            currentCategory = firstCategoryList.get(0);
            //pid=1008002
        }
        //parentCategory
        MarketCategoryExample parentCategoryExample = new MarketCategoryExample();
        parentCategoryExample.createCriteria().andIdEqualTo(currentPid);
        MarketCategory parentCategory = marketCategoryMapper.selectOneByExample(parentCategoryExample);
        //brotherCategory
        MarketCategoryExample brotherCategoryExample = new MarketCategoryExample();
        brotherCategoryExample.createCriteria().andPidEqualTo(currentPid);
        List<MarketCategory> brotherCategory = marketCategoryMapper.selectByExample(brotherCategoryExample);
        //组装
        wxGoodsCategoryVo.setBrotherCategory(brotherCategory);
        wxGoodsCategoryVo.setParentCategory(parentCategory);
        wxGoodsCategoryVo.setCurrentCategory(currentCategory);
        return wxGoodsCategoryVo;
    }

    @Override
    public WxGoodsListVo list(Integer categoryId, Integer page, Integer limit) {
        //categoryId=1008002,根据请求头的categoryId来查询对应的二级类目的所有商品类目
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.createCriteria().andIdEqualTo(categoryId);
        MarketCategory marketCategory = marketCategoryMapper.selectOneByExample(marketCategoryExample);
        //找到了布艺软装这个对象
        Integer categoryPid = marketCategory.getPid();
        //找到了一级类目的Pid
        //filterCategoryList
        MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
        marketCategoryExample1.createCriteria().andPidEqualTo(categoryPid);
        PageHelper.startPage(page, limit);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample1);
        //filterCategoryList响应正常
        //完整的goodsList
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria().andCategoryIdEqualTo(categoryId);
        PageHelper.startPage(page, limit);
//        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        List<MarketGoods> marketGoods1 = marketGoodsMapper.selectByExampleSelective(marketGoodsExample, MarketGoods.Column.brief, MarketGoods.Column.counterPrice, MarketGoods.Column.id, MarketGoods.Column.isHot, MarketGoods.Column.isNew, MarketGoods.Column.name, MarketGoods.Column.picUrl, MarketGoods.Column.retailPrice);
        //组装
        WxGoodsListVo wxGoodsListVo = new WxGoodsListVo();
        wxGoodsListVo.setList(marketGoods1);
        wxGoodsListVo.setFilterCategoryList(marketCategories);
        //组装到Map中，在控制层使用ok响应
        wxGoodsListVo.setPage(page);
        wxGoodsListVo.setLimit(limit);
        return wxGoodsListVo;
    }

    @Override
    public Object count() {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        long count = marketGoodsMapper.countByExample(marketGoodsExample);
        return count;
    }
}
